import queryString from 'query-string';
export default {
  methods: {
    back(delta = 1) {
      uni.navigateBack({
        delta: delta
      })
    },
    openUrl(url) {
      window.open(url)
    },
    navigator(url, param = {}) {
      if (url.indexOf("/") != 0) {
        url = "/" + url
      }

      var gets = '';
      Object.keys(param).forEach(key => {
        if (gets != '') {
          gets += '&'
        }
        const value = typeof(param[key]) == 'object' ? JSON.stringify(param[key]) : param[key]
        gets += key + '=' + value
      })
      if (gets != '') {
        if (url.indexOf("?") > -1) {
          gets = "&" + gets
        } else {
          gets = "?" + gets
        }
      }
      uni.navigateTo({
        url: url + gets
      })
    },
    checkSameUrl(path, to) {
      let flag1 = to == path;
      let flag2 = to == decodeURIComponent(path);
      const toParse = queryString.parseUrl(to);
      const pathParse = queryString.parseUrl(path);
      let flag3 = toParse.url == pathParse.url;
      if (flag3) {
        Object.keys(toParse.query).forEach(key => {
          if (toParse.query[key] != pathParse.query[key]) {
            flag3 = false;
          }
        });
      }

      return flag1 || flag2 || flag3;
    },
	_conditionalParameterAdapter(data, checkedData) {
		if(!checkedData){
			checkedData = [];
		}
		if (Array.isArray(data)) {
			return Object.keys(data).map(key => {
				return {
					text: data[key],
					value: parseInt(key),
					checked:checkedData.indexOf(key) > -1
				}
			})
		} else {
			return Object.keys(data).map(key => {
				return {
					text: data[key],
					value: key,
					checked:checkedData.indexOf(key) > -1
				}
			})
		}
	},
  }
}